#include <string.h>
#include <iostream>
#include "Role.h"
#include "Global.h"

using std::cout; using std::endl;

//显示角色姓名
void CRole::ShowName()
{
#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, NPC_NAME_COLOR|0);
	cout << pNpcs[nNpcIndex].szName;
#else
    printf(NPC_NAME_STRING, szName);
#endif
}


//显示角色绰号
void CRole::ShowNickName()
{
#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, NPC_NAME_COLOR|0);
	cout << szNick;
#else
    printf(NICK_NAME_STRING, szNick);
#endif
}

//显示角色描述
void CRole::ShowDetail()
{
	ShowName();
    cout << " [";
    ShowNickName();
    cout << "] " << endl;

#ifdef _WIN32_
	SetConsoleTextAttribute( hConsoleHandle, DESCRIPTION_COLOR | 0);	
	cout << szDetail << endl;
#else
    printf(DESCRIPTION_STRING, szDetail);
    cout << endl;
#endif
	// cout << "头戴: ";
	// ShowEquipment( nNpcIndex, 0 );
	// cout << endl; 
	// cout << "身着: ";
	// ShowEquipment( nNpcIndex, 1);
	// cout << endl; 
	// cout << "手持: ";
	// ShowEquipment( nNpcIndex, 2 );
	// cout << endl;
	// cout << "脚踏: ";
	// ShowEquipment( nNpcIndex, 3 );
	// cout << endl ;
	
}


void CRole::ShowState(){
	ShowName();
    cout << " [";
    ShowNickName();
    cout << "] " << endl;
    cout << "( 生命：" << nHP << "/" << nMaxHP
         << "； 内力：" << nMP << "/" << nMaxMP
         << "； " << nMoney << "文 )" << endl;
}

/* 显示角色的详细信息 */
void CRole::ShowDetailX(CNickData &nicks)
{
	ShowDetail();	

#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, ITEM_NAME_COLOR| 0);
#endif
	
	cout << "银两: " << nMoney << endl;
	cout << endl;

#ifdef _WIN32
	SetConsoleTextAttribute( hConsoleHandle, DESCRIPTION_COLOR|0);
#endif
    
    for (short i = 0; i < SKILL_KIND; ++i){
        cout << nicks.szSkillNames[i] << "修为  ：" 
             << nExps[i] << endl;
    }
	
	cout << "生命    ："<< nHP 
		<< "\t/ " << nMaxHP << endl;
	cout << "内力    ："<< nMP 
		<< "\t/ "<< nMaxMP << endl;

	cout << "攻击    ："<< nAttack << "\t +" 
		<< GetAddedAttack() << endl;
	cout << "防御    ："<< nDefend << "\t +"
		<< GetAddedDefend() << endl;
	cout << "身法    ："<< nSpeed << "\t +"
         << GetAddedSpeed() << endl << endl;
}

bool CRole::Load(CTextParser &file)
{
	short i;

    file.FindRecordHead();

    //role description
    file.GotoNextLine();
    file.ReadString( szName  );
    file.GotoNextLine();
    file.ReadString( szDetail );
		
    file.GotoNextLine();
    file.ReadInteger( &nExps[0] );	//Exp for sword
    file.ReadInteger( &nExps[1] );	//Exp for blade
    file.ReadInteger( &nExps[2] );	//Exp for long
    file.ReadInteger( &nExps[3] );	//Exp for hand
    // UpdateNick(); //create nick

    file.GotoNextLine();
    file.ReadInteger( &nHP );	//hp
    file.ReadInteger( &nMaxHP );	//maxHP
    file.ReadInteger( &nMP );	//mp
    file.ReadInteger( &nMaxMP );	//maxMp

    file.GotoNextLine();
    file.ReadInteger( &nAttack);	//attack
    file.ReadInteger( &nDefend);	//defend
    file.ReadInteger( &nSpeed );	//speed
		
    //read skill data
    file.GotoNextLine();
    file.ReadInteger( &nSkillNum );	//skill number
    file.GotoNextLine();
    for ( i = 0; i < nSkillNum ; i++ )
    {
        file.ReadInteger( &MySkills[i].nSkillIndex );
        file.ReadInteger( &MySkills[i].nSkillLevel );
        file.ReadInteger( &MySkills[i].nTimes );
    }

    //read goods data
    file.GotoNextLine();
    file.ReadInteger( &nGoodsNum );	//goods number
    file.GotoNextLine();
    for ( i = 0; i < nGoodsNum; i++ )
    {
        file.ReadInteger( &MyGoods[i]);
    }

    //equipment
    file.GotoNextLine();
    for( i = 0; i < 4; ++ i)
        file.ReadInteger( &MyEquipment[i] );
		
    //money
    file.GotoNextLine();
    file.ReadInteger( &nMoney );

    // related script data
    file.GotoNextLine();	
    file.ReadInteger( &nTalkScript ); 	//talk script index
    file.ReadInteger( &nFailScript );	//fail script index				
    return true;
}

void CRole::UpdateNick(CNickData &nicks)
{
    short nMaxIdx = -1;
    short nMaxExp = -1;
    for (short i = 0; i < SKILL_KIND; ++i){
        if (nExps[i] > nMaxExp){
            nMaxExp = nExps[i];
            nMaxIdx = i;
        }
    }
    strcpy(szNick, nicks.szSkillNames[nMaxIdx]);
    for (short i = NICK_LVL -1; i >=0; --i){
        if (nMaxExp >= nicks.nNickExps[i]){
            strcat(szNick, nicks.szLvlNicks[i]);
            break;
        }
    }
}

short CRole::GetAddedAttack()
{
	// short nAdd = 0;
    // short i;
	// for( i =0; i < 4; ++ i )
	// {
	// 	if( pNpcs[nRoleIndex].MyEquipment[i] != -1 )
	// 		nAdd += pGoods[ pNpcs[nRoleIndex].MyEquipment[i] ].nAddAttack ; 
	// }

	// return nAdd;
    // TODO: add attack from equipments
    return 0;
}

short CRole::GetAddedDefend()
{
	// short nAdd = 0;
    // short i;
	// for( i =0; i < 4; ++ i )
	// {
	// 	if( pNpcs[nRoleIndex].MyEquipment[i] != -1 )
	// 		nAdd += pGoods[ pNpcs[nRoleIndex].MyEquipment[i] ].nAddDefence ; 
	// }
	
	// return nAdd;
    // TODO: add attack from equipments
    return 0;
}

short CRole::GetAddedSpeed()
{
	// short nAdd = 0;
    // short i;
	// for( i =0; i < 4; ++ i )
	// {
	// 	if( pNpcs[nRoleIndex].MyEquipment[i] != -1 )
	// 		nAdd += pGoods[ pNpcs[nRoleIndex].MyEquipment[i] ].nAddSpeed ; 
	// }
	
	// return nAdd;	
    // TODO: add attack from equipments
    return 0;
}

void CRole::AddMoney(short n){
    nMoney += n;
    ShowName();
    cout << "得到金钱" << n << "文。" << endl;
    // SystemPause();
}

void CRole::DelMoney(short n){
    if (n > nMoney)
        n = nMoney;

    nMoney -= n;

    ShowName();
    cout << "失去金钱" << n << "文。" << endl;
}

//检查某种物品在玩家身上的位置，如果没有返回-1。
short CRole::GetItem(short nGoodsIndex)
{
	short nLoc;
	for( nLoc = 0; nLoc < nGoodsNum; ++ nLoc)
	{
		if ( MyGoods[nLoc] == nGoodsIndex )
			break;
	}

	if ( nLoc == nGoodsNum )
		return -1;
	else
		return nLoc;
}

// Return the count of a certain item
short CRole::HaveItem(short nGoodsIdx){
    short nNumber = 0;
    for (short i = 0; i < nGoodsNum; ++ i){
        if (MyGoods[i] == nGoodsIdx)
            nNumber ++;
    }
    return nNumber;
}

void CRole::Refresh()
{
    nHP = nMaxHP;
    nMP = nMaxMP;
}
